package xfsale.service.impl.j2ee;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import xfsale.exception.AppException;
import xfsale.packet.TransAcctCancelRequest;
import xfsale.packet.TransAcctCancelResponse;

public final class TransAcctCancel extends BaseOrderAcct {

	private TransAcctCancelRequest request;
	private TransAcctCancelResponse response;

	public TransAcctCancel(TransAcctCancelRequest request, TransAcctCancelResponse response) {
		super(request, response);
		this.request = request;
		this.response = response;
		response.returnCode = "0000";
		response.returnMsg = "交易账户销户成功";
	}

	//检查输入参数
    @Override
	protected void checkData() throws Exception {
		super.checkData();
	}

	//初始化
    @Override
	protected void load() throws Exception {
		super.load();
	}

	//检查功能权限
    @Override
	protected void checkPriv() throws Exception {
		super.checkPriv();
	}

	//检查条件是否满足
    @Override
	protected void checkCondition() throws Exception {
		super.checkCondition();

		PreparedStatement stmt = null;
		ResultSet rs = null;
		int count = 0;
		try {
			//检查资金余额MoneyBal资金余额为0
			logger.debug("检查资金余额...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from MoneyBal " +
				"where transAccountID = ? " +
				"and totalBal <> 0 " +
				"and availableBal <> 0 " +
				"and frozenBal <> 0"
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在未结清的资金余额");
			}

			//检查份额余额ShareBal份额余额为0
			logger.debug("检查份额余额...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from ShareBal " +
				"where transAccountID = ? " +
				"and totalVol <> 0 " +
				"and availableVol <> 0 " +
				"and frozenVol <> 0"
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在未结清的份额余额");
			}

			//检查账户委托OrderAcct无有效委托
			logger.debug("检查账户委托...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from OrderAcct " +
				"where transAccountID = ? " +
				"and orderStatus not in('3', '4') "
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在有效的账户委托");
			}

			//检查交易委托OrderTrans无有效委托
			logger.debug("检查交易委托...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from OrderTrans " +
				"where transAccountID = ? " +
				"and orderStatus not in('3', '4') "
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在有效的交易委托");
			}

			//检查资金委托OrderMoney无有效委托
			logger.debug("检查资金委托...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from OrderMoney " +
				"where transAccountID = ? " +
				"and orderStatus not in('3', '4') "
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在有效的交易委托");
			}

			//检查账户申请AppAcct无记录
			logger.debug("检查账户申请...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from AppAcct " +
				"where transAccountID = ? "
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在账户申请");
			}

			//检查交易申请AppTrans无有效委托
			logger.debug("检查交易申请...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from AppTrans " +
				"where transAccountID = ? "
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("存在交易申请");
			}

			logger.debug("检查交易账户销户条件成功.");
		} catch (Exception e) {
			logger.error("检查交易账户销户条件出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

	//功能处理
    @Override
	protected void process() throws Exception {
		//更新交易账户状态
		updateStatus();

		//写入账户委托数据
		writeOrderAcct();
	}

	//更新交易账户状态
	private void updateStatus() throws Exception {
		PreparedStatement stmt = null;
		try {
			logger.debug("更新交易账户状态...");
			stmt = conn.prepareStatement("" +
				"update AcctTrans set " +
				"transAcctStatus = '1' " +
				"where transAccountID = ? "
			);
			stmt.setString(1, request.transAccountID);
			stmt.executeUpdate();
			logger.debug("更新交易账户状态成功.");
		} catch (Exception e) {
			logger.error("更新交易账户状态出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}


	//写入账户委托数据
	private void writeOrderAcct() throws Exception {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			//生成委托流水
			logger.debug("生成委托流水...");
			serial.setTradeDate(sys.getWorkDay());
			serial.genOrderSerial();
			response.orderSerialNo = serial.getOrderSerialNo();

			//写入账户委托数据
			logger.debug("写入账户委托数据...");
			stmt = conn.prepareStatement(SQL_WRITE_ORDER_ACCT);
            stmt.setString(1, response.orderSerialNo);		//委托流水
			stmt.setString(2, request.serviceCode);			//功能代码
            stmt.setString(3, businessCode);                //业务码
			stmt.setString(4, request.acceptMethod);		//受理方式
			stmt.setString(5, request.regionCode);			//地区编号
			stmt.setString(6, request.branchCode);			//网点编号
            stmt.setString(7, request.netCode);             //销售网点编号
			stmt.setString(8, request.opCode);				//柜员编号
			stmt.setString(9, custAccountID);               //客户帐号
			stmt.setString(10, request.transAccountID);     //交易帐号
			stmt.setString(11, null);						//基金帐号
			stmt.setString(12, null);						//TA代码
			stmt.setString(13, custType);                   //客户类型
			stmt.setString(14, custName);                   //客户名称
			stmt.setString(15, custNameAbbr);               //客户简称
			stmt.setString(16, nationality);                //国籍
			stmt.setString(17, certType);                   //证件类型
			stmt.setString(18, certNo);                     //证件号码
			stmt.setString(19, certValidFrom);              //证件有效期起始日期
			stmt.setString(20, certValidTo);                //证件有效期截至日期
			stmt.setString(21, null);                       //上交所证券帐号
			stmt.setString(22, null);                       //深交所证券帐号
			stmt.setString(23, null);                       //通讯地址
			stmt.setString(24, null);                       //邮政编码
			stmt.setString(25, null);                       //电话号码
			stmt.setString(26, null);                       //住址电话
			stmt.setString(27, null);                       //办公电话
			stmt.setString(28, null);                       //手机号码
			stmt.setString(29, null);                       //传真号码
			stmt.setString(30, null);                       //E-MAIL地址
			stmt.setString(31, null);                       //出生日期
			stmt.setString(32, null);                       //性别
			stmt.setString(33, null);                       //教育程度
			stmt.setString(34, null);                       //职业代码
            stmt.setString(35, null);                       //行业代码
            stmt.setString(36, null);                       //婚姻状况
			stmt.setString(37, null);                       //年收入
			stmt.setString(38, null);                       //机构类型
            stmt.setString(39, null);                       //机构性质
            stmt.setString(40, null);                       //机构注册地
            stmt.setString(41, null);                       //机构注册资本
            stmt.setString(42, null);                       //机构员工数量
			stmt.setString(43, null);                       //法人姓名
			stmt.setString(45, null);                       //法人证件类型
			stmt.setString(46, null);                       //法人证件号码
			stmt.setString(47, null);                       //法人证件有效期起始日期
			stmt.setString(48, null);                       //法人证件有效期截至日期
			stmt.setString(49, request.instTransName);				//经办人姓名
			stmt.setString(50, request.instTransCertType);			//经办人证件类型
			stmt.setString(51, request.instTransCertNo);			//经办人证件号码
			stmt.setString(52, request.instTransCertValidFrom);		//经办人证件有效期起始日期
			stmt.setString(53, request.instTransCertValidTo);		//经办人证件有效期截至日期
			stmt.setString(54, null);                               //经办人联系电话
			stmt.setString(55, null);                               //经办人移动电话
            stmt.setString(55, null);                               //经纪人
			stmt.setString(56, null);                       //银行代码
			stmt.setString(57, null);                       //银行全称
			stmt.setString(58, null);                       //银行帐号户名
			stmt.setString(59, null);                       //银行帐号卡号
            stmt.setString(60, null);                       //银行账号有效期
			stmt.setString(61, null);                       //银行帐号验证
			stmt.setString(62, null);                       //对帐单寄送选择
			stmt.setString(63, null);                       //对账单寄送方式
			stmt.setString(64, null);                       //交易方式
			stmt.setString(65, null);                       //风险承受能力
			stmt.setString(66, null);						//冻结截至日期
			stmt.setString(67, null);						//冻结原因
			stmt.setString(68, null);						//原委托流水
			stmt.setString(69, null);						//原申请流水
			stmt.setString(70, "0");						//委托预约标志
			stmt.setString(71, null);						//委托预约日期
			stmt.setString(72, "0");						//是否需要授权
			stmt.setString(73, null);						//授权标志(0-未授权 1-授权通过 2-授权不通过)
			stmt.setString(74, null);						//授权操作员
			stmt.setString(75, null);						//授权日期
			stmt.setString(76, null);						//授权时间
			stmt.setString(77, "1");						//委托状态
			stmt.setString(78, sys.getWorkDay());			//委托日期
			stmt.setString(79, sys.getWorkTime());			//委托时间
			stmt.setString(80, null);                       //备注
			stmt.executeUpdate();
			logger.debug("写入账户委托数据成功.");
		} catch (Exception e) {
			logger.error("写入账户委托数据出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

}